using System;
using System.Collections;

namespace OracleInternal.Network
{
	internal sealed class NVPair
	{
		internal static int RHS_NONE = 0;

		internal static int RHS_ATOM = 1;

		internal static int RHS_LIST = 2;

		internal static int LIST_REGULAR = 3;

		internal static int LIST_COMMASEP = 4;

		private string m_sName;

		private int m_rhsType;

		private string m_sAtom;

		internal ArrayList m_list;

		private int m_iListType;

		private NVPair m_parent;

		private static string LINESEP = Environment.NewLine;

		private static string ADDRESS = "ADDRESS";

		private static string RULE = "RULE";

		private static string COMMENT = "COMMENT";

		internal string Name
		{
			get
			{
				return m_sName;
			}
			set
			{
				m_sName = value;
			}
		}

		internal NVPair Parent => m_parent;

		internal int RHSType => m_rhsType;

		internal int ListType
		{
			get
			{
				return m_iListType;
			}
			set
			{
				m_iListType = value;
			}
		}

		internal string Atom
		{
			get
			{
				return m_sAtom;
			}
			set
			{
				m_rhsType = RHS_ATOM;
				m_sAtom = value;
				m_list = null;
			}
		}

		internal int ListSize
		{
			get
			{
				if (m_list == null)
				{
					return 0;
				}
				return m_list.Count;
			}
		}

		internal NVPair(string name)
		{
			m_sName = name;
			m_sAtom = null;
			m_list = null;
			m_iListType = LIST_REGULAR;
			m_parent = null;
			m_rhsType = RHS_NONE;
		}

		internal NVPair(string name, string atom)
			: this(name)
		{
			Atom = atom;
		}

		internal NVPair(string name, NVPair child)
			: this(name)
		{
			AddListElement(child);
		}

		private void SetParent(NVPair parent)
		{
			m_parent = parent;
		}

		private bool ContainsComment(string str)
		{
			for (int i = 0; i < str.Length; i++)
			{
				if (str[i] == '#')
				{
					if (i == 0)
					{
						return true;
					}
					if (str[i - 1] != '\\')
					{
						return true;
					}
				}
			}
			return false;
		}

		internal NVPair Clone()
		{
			NVPair nVPair = new NVPair(Name);
			nVPair.Atom = Atom;
			for (int i = 0; i < ListSize; i++)
			{
				nVPair.AddListElement(GetListElement(i).Clone());
			}
			return nVPair;
		}

		internal NVPair GetListElement(int pos)
		{
			if (m_list == null)
			{
				return null;
			}
			return (NVPair)m_list[pos];
		}

		internal void AddListElement(NVPair pair)
		{
			if (m_list == null)
			{
				m_rhsType = RHS_LIST;
				m_list = ArrayList.Synchronized(new ArrayList(3));
				m_sAtom = null;
			}
			m_list.Add(pair);
			pair.SetParent(this);
		}

		internal int AddListElementI(NVPair pair)
		{
			if (m_list == null)
			{
				m_rhsType = RHS_LIST;
				m_list = ArrayList.Synchronized(new ArrayList(3));
				m_sAtom = null;
			}
			int result = m_list.Add(pair);
			pair.SetParent(this);
			return result;
		}

		internal void AddListElement(int after, NVPair pair)
		{
			if (m_list == null)
			{
				m_rhsType = RHS_LIST;
				m_list = ArrayList.Synchronized(new ArrayList(3));
				m_sAtom = null;
			}
			m_list.Insert(after, pair);
			pair.SetParent(this);
		}

		internal void RemoveListElement(int pos)
		{
			if (m_list != null)
			{
				m_list.RemoveAt(pos);
				if (ListSize == 0)
				{
					m_list = null;
					m_rhsType = RHS_NONE;
				}
			}
		}

		internal string ValueToString()
		{
			string text = "";
			if (m_rhsType == RHS_ATOM)
			{
				text += m_sAtom;
			}
			else if (m_rhsType == RHS_LIST)
			{
				if (m_iListType == LIST_REGULAR)
				{
					for (int i = 0; i < ListSize; i++)
					{
						text += GetListElement(i).ToString();
					}
				}
				else if (m_iListType == LIST_COMMASEP)
				{
					for (int j = 0; j < ListSize; j++)
					{
						NVPair listElement = GetListElement(j);
						text += listElement.Name;
						if (j != ListSize - 1)
						{
							text += ", ";
						}
					}
				}
			}
			return text;
		}

		public override string ToString()
		{
			string str = "(" + m_sName + "=";
			if (m_rhsType == RHS_ATOM)
			{
				str += m_sAtom;
			}
			else if (m_rhsType == RHS_LIST)
			{
				if (m_iListType == LIST_REGULAR)
				{
					for (int i = 0; i < ListSize; i++)
					{
						str += GetListElement(i).ToString();
					}
				}
				else if (m_iListType == LIST_COMMASEP)
				{
					str += " (";
					for (int j = 0; j < ListSize; j++)
					{
						NVPair listElement = GetListElement(j);
						str += listElement.Name;
						if (j != ListSize - 1)
						{
							str += ", ";
						}
					}
					str += ")";
				}
			}
			return str + ")";
		}
	}
}
